/*
 * MainWindow.java
 *
 * Created on 2008. február 28., 17:10
 */

package orkomet;

import com.hp.hpl.jena.graph.query.Expression.Application;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  Schimi
 */
public class MainWindow extends javax.swing.JFrame {
    
    boolean isPublished=true;
    TrayIcon trayIcon;
    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();
        initTray();
        mainLogPanel.registerLogger(getClass().getPackage().getName());
        Logger.getLogger(getClass().getName()).log(Level.INFO,java.util.ResourceBundle.getBundle("orkomet/logprops").getString("initStarted"));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
       // bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        popupTray = new java.awt.PopupMenu();
        mniOpen = new java.awt.MenuItem();
        mniStart = new java.awt.MenuItem();
        mniStop = new java.awt.MenuItem();
        mniExit = new java.awt.MenuItem();
        btnStart = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        mainLogPanel = new orkomet.JTableLogHandlerPanel();

        popupTray.setLabel("PopupMenu");

        mniOpen.setLabel("Open");
        mniOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniOpenActionPerformed(evt);
            }
        });
        popupTray.add(mniOpen);
        popupTray.addSeparator();
      //  org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, btnStart, org.jdesktop.beansbinding.ELProperty.create("${enabled}"), mniStart, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
      //  bindingGroup.addBinding(binding);
     //   binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, btnStart, org.jdesktop.beansbinding.ELProperty.create("${text}"), mniStart, org.jdesktop.beansbinding.BeanProperty.create("label"));
     //   bindingGroup.addBinding(binding);

        mniStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniStartActionPerformed(evt);
            }
        });
        popupTray.add(mniStart);

   //     binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, btnStop, org.jdesktop.beansbinding.ELProperty.create("${enabled}"), mniStop, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
   //     bindingGroup.addBinding(binding);
   //     binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, btnStop, org.jdesktop.beansbinding.ELProperty.create("${text}"), mniStop, org.jdesktop.beansbinding.BeanProperty.create("label"));
   //     bindingGroup.addBinding(binding);

        mniStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniStopActionPerformed(evt);
            }
        });
        popupTray.add(mniStop);
        popupTray.addSeparator();
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("orkomet/guiprops"); // NOI18N
        mniExit.setLabel(bundle.getString("menuNameExit")); // NOI18N
        mniExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniExitActionPerformed(evt);
            }
        });
        popupTray.add(mniExit);

        setTitle("Orkomet Service");

        btnStart.setText(bundle.getString("startStateName")); // NOI18N
        btnStart.setEnabled(false);
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        btnStop.setText(bundle.getString("stopStateName")); // NOI18N
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        mainLogPanel.setName(bundle.getString("panelNameLog")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainLogPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnStart)
                    .addComponent(btnStop))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mainLogPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnStart)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStop)
                        .addGap(166, 166, 166)))
                .addContainerGap())
        );

//        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
       actionEndPublish();
    }//GEN-LAST:event_btnStopActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
       actionStartPublish();
    }//GEN-LAST:event_btnStartActionPerformed

    private void mniStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniStartActionPerformed
       actionStartPublish();
    }//GEN-LAST:event_mniStartActionPerformed

    private void mniExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniExitActionPerformed
        actionShutdown();      
    }//GEN-LAST:event_mniExitActionPerformed

    private void mniStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniStopActionPerformed
        actionEndPublish();
    }//GEN-LAST:event_mniStopActionPerformed

    private void mniOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniOpenActionPerformed
       setVisible(true);
    }//GEN-LAST:event_mniOpenActionPerformed
    
    
    
    public void stateChanged()
    {
        isPublished=Main.getInstance().getMainState();
        btnStart.setEnabled(!isPublished);
        btnStop.setEnabled(isPublished);
    }
    
    public void initTray()
    {
         if (SystemTray.isSupported()) {
         // get the SystemTray instance
         SystemTray tray = SystemTray.getSystemTray();
         // load an image
         Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icon.gif"));
         // create a action listener to listen for default action executed on the tray icon
         ActionListener listener = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 setVisible(true);
             }
         };
         // create a popup menu
         trayIcon = new TrayIcon(image, getTitle(), popupTray);
        //  set the TrayIcon properties
         trayIcon.addActionListener(listener);
         // add the tray image
         try {
             tray.add(trayIcon);
         } catch (AWTException e) {
             System.err.println(e);
         }
     } 
    }
   
   
    void actionStartPublish()
    {
        Main.getInstance().startPublish();
    }
    
     void actionEndPublish()
    {
        Main.getInstance().endPublish();
    }
    
     void actionShutdown()
    {
        Main.shutdown();
    }
     
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStop;
    private orkomet.JTableLogHandlerPanel mainLogPanel;
    private java.awt.MenuItem mniExit;
    private java.awt.MenuItem mniOpen;
    private java.awt.MenuItem mniStart;
    private java.awt.MenuItem mniStop;
    private java.awt.PopupMenu popupTray;
//    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    
}
